package org.misty.blog.controller;

import org.misty.blog.exception.NotFoundException;
import org.misty.blog.po.Category;
import org.misty.blog.service.CategoryService;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;
import java.util.Optional;

@Controller
@RequestMapping("/admin")
public class CategoryController {
    private final CategoryService categoryService;

    public CategoryController(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    @GetMapping("/categories")
    public String categories(
            @PageableDefault(size = 10, sort = {"id"}, direction = Sort.Direction.DESC) Pageable pageable,
            Model model
    ) {
        model.addAttribute("page", categoryService.listCategory(pageable));
        return "admin/types";
    }

    @GetMapping("/categories/input")
    public String input(Model model) {
        model.addAttribute("category", new Category());
        return "admin/types-input";
    }

    @GetMapping("/categories/{id}/input")
    public String edit(@PathVariable Long id, Model model) {
        Optional<Category> old = categoryService.getCategory(id);
        if (!old.isPresent()) {
            throw new NotFoundException("<分类>找不到当前对象: " + id);
        }
        model.addAttribute("category", old.get());
        return "admin/types-input";
    }

    @PostMapping("/categories")
    public String post(@Valid Category category, BindingResult result, RedirectAttributes attributes) {
        Optional<Category> old = categoryService.getCategoryByName(category.getName());
        if (old.isPresent()) {
            result.rejectValue("name", "nameError", "分类名称已存在: " + category.getName());
        }
        if (result.hasErrors()) {
            return "admin/types-input";
        }
        Category ret = categoryService.saveCategory(category);
        if (ret == null) {
            attributes.addFlashAttribute("message", "新增失败");
        } else {
            attributes.addFlashAttribute("message", "新增成功");
        }
        return "redirect:/admin/categories";
    }

    @PostMapping("/categories/{id}")
    public String editPost(@PathVariable Long id, @Valid Category category, BindingResult result, RedirectAttributes attributes) {
        Optional<Category> old = categoryService.getCategoryByName(category.getName());
        if (old.isPresent()) {
            result.rejectValue("name", "nameError", "分类名称已存在: " + category.getName());
        }
        if (result.hasErrors()) {
            return "admin/types-input";
        }
        Category ret = categoryService.updateCategory(id, category);
        if (ret == null) {
            attributes.addFlashAttribute("message", "修改失败");
        } else {
            attributes.addFlashAttribute("message", "修改成功");
        }
        return "redirect:/admin/categories";
    }

    @RequestMapping("/categories/{id}/delete")
    public String delete(@PathVariable Long id, RedirectAttributes attributes) {
        categoryService.removeCategory(id);
        attributes.addFlashAttribute("message", "删除成功");
        return "redirect:/admin/categories";
    }
}
